﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;
using Microsoft.Xna.Framework.Content;
using FarseerGames.FarseerPhysics.Factories;
using FarseerGames.FarseerPhysics;

namespace SimpleGame
{
    public class Sprite
    {

        protected Texture2D currentTexture;
        protected Vector2 position;
        protected Body body;
        protected Geom geom;
        protected Vector2 origin;
        protected float rotation;


        public Body Body
        {
            get { return body; }
        }

        public Geom Geom
        {
            get { return geom; }
        }

        public Vector2 Origin
        {
            get { return origin; }
        }

        public Texture2D CurrentTexture
        {
            get { return currentTexture; }
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        public Sprite()
        {
            position = new Vector2(0.0f, 0.0f);
            rotation = 0.0f;
            origin = new Vector2(0.0f, 0.0f);
            currentTexture = null;
            body = null;
            geom = null;
        }

        public bool IsStatic
        {
            get { return body.IsStatic; }
            set { body.IsStatic = value; }
        }

        public virtual bool Load(string assetName, ContentManager content, PhysicsSimulator simulator)
        {
            currentTexture = content.Load<Texture2D>(assetName);
            origin = new Vector2(currentTexture.Width / 2, currentTexture.Height / 2);
            body=BodyFactory.Instance.CreateRectangleBody(currentTexture.Height, currentTexture.Width, 1);
            geom = GeomFactory.Instance.CreateRectangleGeom(body, currentTexture.Width, currentTexture.Height);
            body.Position = position;
            body.Tag = this;
            geom.Tag = this;
            simulator.Add(body);
            simulator.Add(geom);
            return true;
        }

        public virtual void Update(GameTime time)
        {
            position.X = body.Position.X;
            position.Y = body.Position.Y;
            rotation = body.Rotation;
        }
    }
}
